﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public abstract class Ammunition : MonoBehaviour{
    public AmmunitionProperty ammunitionProperty;//弹药的属性

    protected DestroyPoolObject destroyPoolObject;//用于回收子弹的脚本
    protected Vector3 rightDir;//子弹的右方向，上次的方向
    protected Collider2D[] gosInRange;//附近的2D碰撞体数组
    protected string otherTag;//对方的Tag
    protected float distance;//两点的距离
    protected void Start()
    {
        destroyPoolObject = GetComponent<DestroyPoolObject>();
        otherTag = ammunitionProperty.ReturnOtherTag(this.tag);
    }
    /// <summary>
    /// 设置子弹的右方向
    /// </summary>
    /// <param name="trans">Trans.</param>
    public void SetDirection(Transform trans)
    {
        rightDir = trans.right;
        if(rightDir.x == 1)
        {
            transform.rotation = Quaternion.AngleAxis(0, transform.up);
        }
        else if(rightDir.x == -1)
        {
            transform.rotation = Quaternion.AngleAxis(180, transform.up);
        }
    }
    /// <summary>
    /// 渐变伤害方法
    /// </summary>
    public int RangeHurt(int damage, float range,Transform thisTrans)
    {
        if (range > 0.1f)
        {
            gosInRange = Physics2D.OverlapCircleAll(thisTrans.position,range);
            foreach (Collider2D go in gosInRange)
            {
                distance = Vector2.Distance(thisTrans.position,go.transform.position);
                if((1 - (distance / range) > 0)){
                    damage = (int)(damage * (1 - (distance / range)));
                }else
                {
                    damage = 0;
                }
            }
            return damage;
        }
        else
        {
            return damage;
        }
    }
    protected void OnTriggerEnter2D(Collider2D other)
    {
        if (other.CompareTag(otherTag) && other.GetComponent<Health>() != null)
        {
            other.GetComponent<Health>().FeelHurt(RangeHurt(ammunitionProperty.damage, ammunitionProperty.range, transform));
        }

        if (other.CompareTag(this.tag))
        {
            return;
        }
        destroyPoolObject.objectPool.ReturnObjcet(gameObject);
    }

    public abstract void AmmunitionMove();// 弹药的移动轨迹
}
